package net.groupme.gui.rendering;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import de.nava.informa.impl.basic.Item;

import net.groupme.gui.rendering.constants.RENDERING_CONSTANTS;
import net.groupme.model.ResourceVisualization;

/**
 * created on Jun 29, 2007
 * 
 * This {@link Renderer} implementation renders Resources for the "Big-View"
 * in the centre of the browser.
 * 
 * @author Mischa Frank, <a href="code@planetfrank.net">code@planetfrank.net</a>
 * @author last edited by: $Author: mischa $
 * 
 * @version $Revision: 1.10 $ $Date: 2007-10-31 16:03:04 $
 */
public class BigResourceRenderer extends AbstractRenderer {

	/* (non-Javadoc)
	 * @see net.groupme.gui.rendering.AbstractRenderer#renderAsHTML(net.groupme.model.ResourceVisualization)
	 */
	@Override
	public String renderAsHTML(ResourceVisualization resourceVisualization,	Integer windowWidth, Integer windowHeight, Integer zAxis) {
		String bigResourceHTML = "";
		
		if (resourceVisualization.getRenderer().equals(RENDERING_CONSTANTS.RENDERER_IMAGE)){
			bigResourceHTML = this.getBigResourceVisualizationHead(resourceVisualization, windowWidth, windowHeight);
			
			bigResourceHTML += "<strong>" + resourceVisualization.getResource().getName() + "</strong>" + "<br/>";
			if (resourceVisualization.getHeight() > (resourceVisualization.getWidth()+100)){
				bigResourceHTML += "<img style=\"height:" + ((windowHeight*0.8)-150) + "px; max-width:" + (windowWidth*0.5) + "px;\" id=\"" + resourceVisualization.getURI().toString() + "_content" + "\" src=\"" + resourceVisualization.getResource().getURL().toString() + "\" alt=\"" + resourceVisualization.getResource().getName() + "\"/>";
			}
			else{
				bigResourceHTML += "<img style=\"width:" + (windowWidth*0.5) + "px;\" id=\"" + resourceVisualization.getURI().toString() + "_content" + "\" src=\"" + resourceVisualization.getResource().getURL().toString() + "\" alt=\"" + resourceVisualization.getResource().getName() + "\"/>";
			}
			// close div _content
			bigResourceHTML += "</div>";
		}
		if (resourceVisualization.getRenderer().equals(RENDERING_CONSTANTS.RENDERER_DEFAULT)){
			bigResourceHTML = this.getBigResourceVisualizationHead(resourceVisualization, windowWidth, windowHeight);
			bigResourceHTML += "<div id=\"" + resourceVisualization.getURI().toString() + "_content\" style=\"width:" + (windowWidth*0.5) + "px; min-width:130px; background:white; font-size:1.2em;\"> <strong>" + resourceVisualization.getResource().getName() + "</strong>" + "<br/>";
			
			if(resourceVisualization.getResource().getDescription() != null){
				bigResourceHTML +=  "Description: " + resourceVisualization.getResource().getDescription() ;
			}
			
			if(resourceVisualization.getResource().getURL() != null){
				bigResourceHTML += "<br/>URL: <a href=\"" + resourceVisualization.getResource().getURL() +"\" target=\"_blank\""+ "\" title=\"visit resource @ " + resourceVisualization.getResource().getURL() +"\">"
					 + formatURL(resourceVisualization.getResource().getURL())
					 + "</a>";
			}
			// close div _content
			bigResourceHTML += "</div>";
		}
		if (resourceVisualization.getRenderer().equals(RENDERING_CONSTANTS.RENDERER_RSS)){
			bigResourceHTML = this.getBigResourceVisualizationHead(resourceVisualization, windowWidth, windowHeight);
			bigResourceHTML += "<div id=\"" + resourceVisualization.getURI().toString() + "_content\" style=\"width:" + (windowWidth*0.5) + "px; min-width:130px; background:white; font-size:1.2em;\"> <strong>" + resourceVisualization.getResource().getName() + "</strong>" + "<br/>";
			
			if(resourceVisualization.getResource().getDescription() != null){
				bigResourceHTML +=  "Description: " + resourceVisualization.getResource().getDescription() ;
			}
			
			if(resourceVisualization.getResource().getURL() != null){
				bigResourceHTML += "<br/>URL: <a href=\"" + resourceVisualization.getResource().getURL() +"\" target=\"_blank\""+ "\" title=\"visit resource @ " + resourceVisualization.getResource().getURL() +"\">"
					 + formatURL(resourceVisualization.getResource().getURL())
					 + "</a>";
			}
			
			if(resourceVisualization.getResource().getURL() != null){
				List<Item> itemList = new RssRenderer().getLatestItems(resourceVisualization.getResource().getURL().toString());
				int i = 0;
				while (i < 10 && i < itemList.size()) {
					bigResourceHTML += "<br/> <a href=\"" + itemList.get(i).getLink() + "\" target=\"_blank\""+ "\" title=\"visit feed @ " + itemList.get(i).getLink() + "\">" 
					+ (i+1) + ". - " + itemList.get(i).getTitle() + "</a>";
					i++;
				}
			}
			// close div _content
			bigResourceHTML += "</div>";
		}
		if (resourceVisualization.getRenderer().equals(RENDERING_CONSTANTS.RENDERER_GROUP)){
			bigResourceHTML = this.getBigResourceVisualizationHead(resourceVisualization, windowWidth, windowHeight);
			bigResourceHTML += "<div id=\"" + resourceVisualization.getURI().toString() + "_content\" style=\"width:" + (windowWidth*0.5) + "px; min-width:130px; background:white; font-size:1.2em;\"> <strong>" + resourceVisualization.getResource().getName() + "</strong>" + "<br/>";
			
			if(resourceVisualization.getResource().getDescription() != null){
				bigResourceHTML +=  "Description: " + resourceVisualization.getResource().getDescription() ;
			}
			/** HACK **/
			URL groupURL = null;
			URL rssURL = null;
			try {
				groupURL = new URL("http://groupme.org/GroupMe/browse-groups.do?id=" + resourceVisualization.getResource().getId());
				rssURL = new URL("http://groupme.org/GroupMe/api/export.do?groupID=" + resourceVisualization.getResource().getId() + "&mode=export-groups&export-format=rss");
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			/** END HACK **/
			if(resourceVisualization.getResource().getURL() != null){
				bigResourceHTML += "<br/>URL: <a href=\"" + groupURL.toString() +"\" target=\"_blank\""+ "\" title=\"visit resource @ " + groupURL +"\">"
					 + formatURL(resourceVisualization.getResource().getURL())
					 + "</a>";
			}
			
			if(resourceVisualization.getResource().getURL() != null){
				List<Item> itemList = new RssRenderer().getLatestItems(rssURL.toString());
				int i = 0;
				while (i < 10 && i < itemList.size()) {
					bigResourceHTML += "<br/> <a href=\"" + itemList.get(i).getLink() + "\" target=\"_blank\""+ "\" title=\"visit feed @ " + itemList.get(i).getLink() + "\">" 
					+ (i+1) + ". - " + itemList.get(i).getTitle() + "</a>";
					i++;
				}
			}
			// close div _content
			bigResourceHTML += "</div>";
		}
		if (resourceVisualization.getRenderer().equals(RENDERING_CONSTANTS.RENDERER_SHOCKWAVE)){
			URL url = resourceVisualization.getResource().getURL();
			if (url.toString().startsWith("http://www.youtube.com/watch?v=")){
				try {
					url = new URL("http://www.youtube.com/v/" + url.toString().substring(url.toString().indexOf("=")+1, url.toString().lastIndexOf("&")));
				} catch (MalformedURLException e) {
					e.printStackTrace();
				}
			}
			bigResourceHTML = this.getBigResourceVisualizationHead(resourceVisualization, windowWidth, windowHeight);
			
			bigResourceHTML += "<strong>" + resourceVisualization.getResource().getName() + "</strong>" + "<br/>";
			bigResourceHTML += "<object>";
			bigResourceHTML += 	"<param name=\"movie\" value=\"" + url + "\"></param>";
			bigResourceHTML += 	"<param name=\"wmode\" value=\"transparent\"></param>";
			bigResourceHTML += 	"<embed id=\"" + resourceVisualization.getURI().toString() + "_content\" src=\"" + url + "\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" style=\"width:" + (windowWidth*0.5) + "px; height:" + (windowWidth*0.5)*0.7787 +  "px;\">";
			bigResourceHTML += 	"</embed>";
			bigResourceHTML += "</object>";
			// close div _content
			bigResourceHTML += "</div>";
		}
		if (resourceVisualization.getRenderer().equals(RENDERING_CONSTANTS.RENDERER_VIDEO) ){
			bigResourceHTML = this.getBigResourceVisualizationHead(resourceVisualization, windowWidth, windowHeight);
			//video
			bigResourceHTML += "<strong>" + resourceVisualization.getResource().getName() + "</strong>" + "<br/>";
			bigResourceHTML += "<object id=\"" + resourceVisualization.getURI().toString() + "_contentVideo\" style=\"width:"+ (windowWidth*0.4) + "px; height:" + (windowWidth*0.4*0.88372) + "px; min-width:150px;\" classid=\"CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6\" " +
					"codebase=\"http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701\" " +
					"standby=\"Loading Microsoft Windows Media Player components...\" >";
			bigResourceHTML += "<param name=\"URL\" value=\"" + resourceVisualization.getResource().getURL() + "\">";
			bigResourceHTML += "<param name=\"SendPlayStateChangeEvents\" value=\"True\">";
			bigResourceHTML += "<param name=\"AutoStart\" value=\"False\">";
			bigResourceHTML += "<param name=\"stretchToFit\" value=\"true\">";
			bigResourceHTML += "<param name=\"uiMode\" value=\"full\">";
			bigResourceHTML += "<param name=\"PlayCount\" value=\"1\">";
			bigResourceHTML += "<param name=\"AutoRewind\" value=\"0\">";
			bigResourceHTML += "<param name=\"AutoSize\" value=\"0\">";
			bigResourceHTML += "<embed id=\"" + resourceVisualization.getURI().toString() + "_contentVideo\" type=\"application/x-mplayer2\" pluginspage=\"http://www.microsoft.com/Windows/MediaPlayer/\" " +
						"name=\"mediaplayer1\" autostart=\"false\" transparentstart=\"1\" style=\"width:"+ (windowWidth*0.4) + "px; height:" + (windowWidth*0.4*0.88372) + "px;\"" +
						"showstatusbar=\"1\" loop=\"0\" controller=\"true\" src=\"" + resourceVisualization.getResource().getURL() + "\" stretchToFit=\"true\" AutoSize=\"0\" Autostart=\"false\" style=\"min-width:150px;\"></embed>";
			bigResourceHTML += "</object>";
			// close div _content
			bigResourceHTML += "</div>";
		}
		return bigResourceHTML;
	}
	
	/**
	 * Transforms a URL to a string with a maximal length. Example:
	 * <pre>
	 * http://abc.de/bliblablup/bliblablup/feuerwehrmann.html
	 * </pre>
	 * ...will be transformed into:
	 * <pre>
	 * ..lup/feuerwehrmann.html
	 * </pre>
	 * @param url the url to be formatted
	 * @return a shortened version of the URL as a String
	 */
	private static final String formatURL(URL url){
		String formatted = url.toString(); 
		if(formatted.length() > 40){
			formatted = "..." + formatted.substring(formatted.length() - 39);
		}
		return formatted;
			
	}

}
